package com.mycompany.virtualApp.controller;

import com.mycompany.mvc.utils.BaseResult;
import com.mycompany.mvc.utils.DateUtil;
import com.mycompany.mvc.utils.SystemUtil;
import com.mycompany.virtualApp.entity.*;
import com.mycompany.virtualApp.service.BannerAdvService;
import com.mycompany.virtualApp.service.ClickService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created by Administrator on 2017/12/6 0006.
 * tab广告栏的controller
 */

@Controller
@RequestMapping("/bannerAdv")
public class BannerAdvController {

    Logger logger = LoggerFactory.getLogger(BannerAdvController.class);

    @Autowired
    private BannerAdvService bannerAdvService;
    @Autowired
    private ClickService clickService;

    /**
     * 获取Banner广告的数据所有数据
     */
    @RequestMapping("/getBannerAdvList")
    @ResponseBody
    public BaseResult<BannerAdvModel> getAllBannerAdv() {
        BaseResult baseResult = new BaseResult();
        List<BannerAdvModel> list = new ArrayList<>();

        try {
            list = bannerAdvService.getBannerAdvList(new HashMap<>());
            baseResult.setList(list);
        } catch (Exception e) {
            logger.error("tab广告栏获取失败!"+e.getMessage());
            baseResult.setSuccess(false);
            return baseResult;
        }

        baseResult.setSuccess(true);
        return baseResult;
    }

    /**
     *  保存新增的Banner广告
     */
    @RequestMapping("/saveBannerAdv")
    @ResponseBody
    public BaseResult saveBannerAdv(HttpServletRequest request) {
        BaseResult baseResult = new BaseResult();
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        MultipartFile prodPicFile = multipartRequest.getFile("prodPic");
        Map<String,Object> prodPicMap = SystemUtil.saveFile(prodPicFile,multipartRequest);
        String prodPic = MapUtils.getString(prodPicMap,"savePath");
        String prodName = multipartRequest.getParameter("prodName");
        String prodUrl = multipartRequest.getParameter("prodUrl");
        String prodRadio = multipartRequest.getParameter("prodRadio");
        String weight = multipartRequest.getParameter("weight");
        if(StringUtils.isAnyEmpty(prodPic,prodName,prodUrl,prodRadio)){
            logger.error("Banner广告保存失败:图片保存失败!");
            baseResult.setSuccess(false);
            return  baseResult;
        }

        BannerAdvModel bannerAdvModel = new BannerAdvModel();
        bannerAdvModel.setAdvName(prodName);
        bannerAdvModel.setAdvImage(prodPic);
        bannerAdvModel.setAdvUrl(prodUrl);
        bannerAdvModel.setWeight(weight);
        bannerAdvModel.setCreateTime(new Date());
        bannerAdvModel.setUpdateTime(new Date());
        try {
            bannerAdvService.saveBannerAdv(bannerAdvModel);
            //更改字典表，全局显示majt
        } catch (Exception e) {
            logger.error("Banner广告保存失败:数据库插入失败!");
            baseResult.setSuccess(false);
            return  baseResult;
        }

        baseResult.setSuccess(true);
        return baseResult;
    }

    /**
     *  获取Banner广告的点击次数
     */
    @RequestMapping("/editClickNums")
    @ResponseBody
    public BaseResult editClickNums(HttpServletRequest request) {
        BaseResult<ClickDetails> baseResult = new BaseResult();
        String flag = request.getParameter("flag");
        String advId = request.getParameter("id");

        //获取对应id和flag的所有记录
        List<ClickDetails> clickDetailsList = new ArrayList<>();
        Map<String,Object> map = new HashMap<>();
        map.put("flag",flag);
        map.put("advId",advId);

        try {
            clickDetailsList = clickService.selectByParam(map);
            baseResult.setSuccess(true);
            baseResult.setList(clickDetailsList);
        } catch (Exception e) {
            logger.error("查看点击详情失败!"+e.getMessage()+"flag:"+flag);
            baseResult.setSuccess(false);
            return baseResult;
        }

        baseResult.setSuccess(true);
        return baseResult;
    }

    /**
     *  获取单个广告信息
     */
    @RequestMapping("/editBannerV")
    @ResponseBody
    public BaseResult<BannerAdvModel> editBannerV(HttpServletRequest request, @RequestParam Integer id) {
        BaseResult<BannerAdvModel> baseResult = new BaseResult();

        Map<String,Object> map = new HashMap<>();
        map.put("id",id);

        try {
            List<BannerAdvModel> bannerAdvModelList = bannerAdvService.getBannerAdvList(map);
            if(CollectionUtils.isNotEmpty(bannerAdvModelList)){
                BannerAdvModel bannerAdvModel =  bannerAdvModelList.get(0);
                baseResult.setSuccess(true);
                baseResult.setT(bannerAdvModel);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("编辑广告失败!");
            baseResult.setSuccess(false);
            return baseResult;
        }

        return baseResult;
    }

    /**
     *  更新单个广告信息
     */
    @RequestMapping("/editBanner")
    @ResponseBody
    public BaseResult<BannerAdvModel> editBanner(HttpServletRequest request, @RequestParam Integer id) {
        BaseResult<BannerAdvModel> baseResult = new BaseResult();

        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
//        MultipartFile prodPicaFile = multipartRequest.getFile("prodPica");
//        MultipartFile prodPicFile = multipartRequest.getFile("prodPic");
//        Map<String,Object> prodPicMap = SystemUtil.saveFile(prodPicFile,multipartRequest);
//        String prodPic = MapUtils.getString(prodPicMap,"savePath");

        String prodUrlbb = multipartRequest.getParameter("prodUrlbb");
        String prodName = multipartRequest.getParameter("prodName");
        String prodUrl = multipartRequest.getParameter("prodUrl");

        String prodRadio = multipartRequest.getParameter("prodRadio");
        String weight = multipartRequest.getParameter("weight");

        BannerAdvModel bannerAdvModel = new BannerAdvModel();
//        bannerAdvModel.setAdvImage(prodPic);
        bannerAdvModel.setAdvName(prodName);
        bannerAdvModel.setAdvImage(prodUrlbb);
        bannerAdvModel.setAdvUrl(prodUrl);
        bannerAdvModel.setId(id);
        bannerAdvModel.setUpdateTime(new Date());
        bannerAdvModel.setWeight(weight);
        try {
            bannerAdvService.updateBanner(bannerAdvModel);
            baseResult.setSuccess(true);
            baseResult.setT(bannerAdvModel);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("编辑广告失败!");
            baseResult.setSuccess(false);
            return baseResult;
        }

        return baseResult;
    }
}
